import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

import '../../services/address.dart';
import '../../services/dio_manager.dart';
import 'office_supplies_model.dart';
import 'office_top_dialog_page.dart';

class OfficeSuppliesController extends GetxController{

  var searchTextEditingController = TextEditingController();

  var easyRefreshController = EasyRefreshController();

  int page = 1;
  List dataArr = [];
  var status;

  var isTopShow = false;

  var replyType = '待领取';

  /// 点击的弹框
  topClick() async {
    isTopShow = true;

    print(replyType);
    var a;
    if (replyType == '待领取') {
      a = 0;
    }
    if (replyType == '已领取') {
      a = 1;
    }

    var data =
    await Get.dialog(OfficeSupliesTopDialogPage(a), barrierColor: Colors.transparent);
    if (data != null) {
      print(data);
      isTopShow = data['isShow'];
      replyType = data['text'];
      status = data['reply_type'];
      page = 1;
      requestData();
    }
    update();
  }


  onRefresh()async{
    page = 1 ;
    requestData();
  }
  onLoad()async{
    page++;
    requestData();
  }


  requestData({var search})async{
    var params = {
      'page':page,
      'limit':10,
      'status':status, //状态:待领取，1-已领取
    };
    var json = await DioManager().kkRequest(Address.officeUser,params: params,method: 'get');
    OfficeSuppliesModel model = OfficeSuppliesModel.fromJson(json);

    if(page == 1){
      easyRefreshController.resetLoadState();
      dataArr.clear();
    }
    if(model.data!=null){
      dataArr.addAll(model.data!);
    }else if(model.data == null){
      BotToast.showText(text: '暂无更多');
      easyRefreshController.finishLoad(noMore: true);
    }
    update();
  }

  requestDataWithUnlink(var office_use_id )async{

    var params = {
      'office_use_id':office_use_id,
    };

    var json = await DioManager().kkRequest(Address.officeUnlink,bodyParams: params);
    // if(json['errmsg'] == 'ok'){
    //   Get.back(result: 'refresh');
    // }
    BotToast.showText(text: json['message']);
  }


  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    requestData();
  }
}